package com.xckj.cycling.service;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.xckj.cycling.bean.common.EventBusBean;
import com.xckj.cycling.bean.common.EventTag;

import org.greenrobot.eventbus.EventBus;

/**
 * 监听蓝牙打开广播
 */
public class BluetoothMonitorReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(action != null){
            switch (action) {
                case BluetoothAdapter.ACTION_STATE_CHANGED:
                    int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                    switch (blueState) {
                        case BluetoothAdapter.STATE_TURNING_ON:
                            Toast.makeText(context,"BluetoothMonitorReceiver：蓝牙正在打开",Toast.LENGTH_SHORT).show();
                            break;
                        case BluetoothAdapter.STATE_ON:
                            Toast.makeText(context,"BluetoothMonitorReceiver：蓝牙已经打开",Toast.LENGTH_SHORT).show();
                            EventBus.getDefault().post(new EventBusBean(EventTag.EventTag_ble_turn_on));
                            break;
                        case BluetoothAdapter.STATE_TURNING_OFF:
                            Toast.makeText(context,"BluetoothMonitorReceiver：蓝牙正在关闭",Toast.LENGTH_SHORT).show();
                            break;
                        case BluetoothAdapter.STATE_OFF:
                            Toast.makeText(context,"BluetoothMonitorReceiver：蓝牙已经关闭",Toast.LENGTH_SHORT).show();
                            EventBus.getDefault().post(new EventBusBean(EventTag.EventTag_ble_turn_off));
                            break;
                    }
                    break;

                case BluetoothDevice.ACTION_ACL_CONNECTED:
                    Toast.makeText(context,"BluetoothMonitorReceiver：蓝牙设备已连接",Toast.LENGTH_SHORT).show();
                    break;
                case BluetoothDevice.ACTION_ACL_DISCONNECTED:
                    Toast.makeText(context,"BluetoothMonitorReceiver：蓝牙设备已断开",Toast.LENGTH_SHORT).show();
                    break;
            }

        }
    }
}